package com.ProcessManagement;

import com.ui.ProcessSchedulerUI;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class PCB {

//    进程编号ProID：值分别为1,2,3,4,5,6等
    private int ProID;
//    进程优先级：Priority
    private int Priority;
//    进程创建时间：StartTimes
    private int StartTimes;
//    进程结束时间：EndTimes
    private int EndTimes;
//    进程状态：PSW
    private String PSW;
//    进程剩余时间统计：TurnTimes,预估要执行的时间
    private int TurnTimes;
//    进程总执行时间
    private int TotalTimes;
//    进程包含的指令数：InstructNum,假设一条指令为要1秒
    private int InstructNum;
//    指令列表
    private List<Instruction> pcbInfoList;

//    若优先级为0，则根据传入的参数生成随机的优先级。其他属性的初始值根据需求进行设定。
    public PCB(int proID, int startTimes, int instructNum, int Priority) {
        this.ProID = proID;
        if (Priority ==0 ){
            this.Priority = generateRandomPriority();
        }else {
            this.Priority = Priority;
        }
        this.StartTimes = startTimes;
        this.EndTimes = 0;
        this.PSW = "Ready";
        this.TurnTimes = 0;
        this.InstructNum = instructNum;
//        指令列表
        pcbInfoList = new ArrayList<>();
    }

//    进程优先数Priority:因为并发数默认最大为5，所以随机生成[1-5]整数优先数
    private int generateRandomPriority() {
        Random random = new Random();
        return random.nextInt(5) + 1;
    }

    public int getProID() {
        return ProID;
    }

    public int getPriority() {
        return Priority;
    }

    public int getStartTimes() {
        return StartTimes;
    }

    public int getEndTimes() {
        return EndTimes;
    }

    public void setEndTimes(int endTimes) {
        EndTimes = endTimes;
    }

    public String getPSW() {
        return PSW;
    }

    public void setPSW(String PSW) {
        this.PSW = PSW;
    }

    public int getTurnTimes() {
        return TurnTimes;
    }

    public void setTurnTimes() {
        int totalTime = 0;
        for (Instruction instruction : pcbInfoList) {
            if (instruction.getInstructionType() == 0) totalTime = totalTime + 1;
            else if (instruction.getInstructionType() == 1) totalTime = totalTime + 2;
        }
        TurnTimes = totalTime;
    }

    public int getTotalTimes() {
        return TotalTimes;
    }

    public void setTotalTimes() {TotalTimes = EndTimes - StartTimes;}

    public int getInstructNum() {
        return InstructNum;
    }

    public List<Instruction> getPcbInfoList() { return pcbInfoList; }

    public void addPcbInfoList(Instruction instruction) {
        pcbInfoList.add(instruction);
    }

//    打印pcb详细信息
    public void printPCBInfo() {
        ProcessSchedulerUI.updatePCBList("进程ID: " + ProID);
        ProcessSchedulerUI.updatePCBList("优先级: " + Priority);
        ProcessSchedulerUI.updatePCBList("开始执行时间: " + StartTimes);
        ProcessSchedulerUI.updatePCBList("结束时间: " + EndTimes);
        ProcessSchedulerUI.updatePCBList("进程状态: " + PSW);
        ProcessSchedulerUI.updatePCBList("进程总时间: " + TotalTimes);
        ProcessSchedulerUI.updatePCBList("进程包含的指令数目: " + InstructNum);
        ProcessSchedulerUI.updatePCBList("\n");
    }

//    打印pcb简单信息
public void printPCB() {
    ProcessSchedulerUI.updateReadyQueue("进程ID: " + ProID +"进程状态: " + PSW);
}

//    打印指令列表
    public void printInstructInfo() {
        for (Instruction instruction : pcbInfoList) {
            System.out.println("指令序号："+instruction.getInstructionNumber()+"指令类型："+instruction.getInstructionType());
        }
    }
}
